﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "ServerConsole.h"
#include "CenterHandler.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CCenterHandler
CCenterHandler::CCenterHandler(void)
: m_uCenterOnline(0)
, m_uCenterTotal(0)
, m_uCenterLoad(0)
, m_uBalanceCount(0)
, m_uBalanceAddr(0)
, m_uBalanceNotice(0)
, m_uAuthorDBCount(0)
, m_uAuthorDBServer(0)
, m_uAuthorDBQuery(0)
, m_uAuthorCount(0)
, m_uAuthorOnline(0)
, m_uAuthorTotal(0)
, m_uMasterDBCount(0)
, m_uMasterDBServer(0)
, m_uMasterDBQuery(0)
, m_uMasterCount(0)
, m_uMasterGate(0)
, m_uMasterService(0)
{
}

CCenterHandler::~CCenterHandler(void)
{
}

void CCenterHandler::OnLive(bool bStart)
{
    CStringFix strTemp = GetName();
    if (bStart)
    {
        strTemp += TF("Start---");

        CStrAddr strAddr;
        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("TCP Int-Listen=[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else
    {
        strTemp += TF("Stop");

        m_uCenterOnline   = 0;
        m_uCenterTotal    = 0;
        m_uCenterLoad     = 0;
        m_uBalanceCount   = 0;
        m_uBalanceAddr    = 0;
        m_uBalanceNotice  = 0;
        m_uAuthorDBCount  = 0;
        m_uAuthorDBServer = 0;
        m_uAuthorDBQuery  = 0;
        m_uAuthorCount    = 0;
        m_uAuthorOnline   = 0;
        m_uAuthorTotal    = 0;
        m_uMasterDBCount  = 0;
        m_uMasterDBServer = 0;
        m_uMasterDBQuery  = 0;
        m_uMasterCount    = 0;
        m_uMasterGate     = 0;
        m_uMasterService  = 0;
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CCenterHandler::OnLink(Int nServerIndex, uintptr_t utData)
{
    AddServerData(nServerIndex, utData);
}

void CCenterHandler::OnUpdate(Int nServerIndex, uintptr_t utData)
{
    UpdateServerData(nServerIndex, utData);
}

void CCenterHandler::OnSync(Int nServerIndex, uintptr_t utData)
{
    PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
    switch (nServerIndex)
    {
    case PAKT_MASTER:
        {
            m_uMasterGate    = pStatus->uTotal;
            m_uMasterService = pStatus->uOnline;
            m_uMasterCount   = pStatus->usLoad;
        }
        break;
    case PAKT_MASTERDB:
        {
            m_uMasterDBServer = pStatus->uTotal;
            m_uMasterDBQuery  = pStatus->uOnline;
            m_uMasterDBCount  = pStatus->usLoad;
        }
        break;
    case PAKT_AUTHOR:
        {
            m_uAuthorTotal  = pStatus->uTotal;
            m_uAuthorOnline = pStatus->uOnline;
            m_uAuthorCount  = pStatus->usLoad;
        }
        break;
    case PAKT_AUTHORDB:
        {
            m_uAuthorDBServer = pStatus->uTotal;
            m_uAuthorDBQuery  = pStatus->uOnline;
            m_uAuthorDBCount  = pStatus->usLoad;
        }
        break;
    case PAKT_BALANCE:
        {
            m_uBalanceAddr   = pStatus->uTotal;
            m_uBalanceNotice = pStatus->uOnline;
            m_uBalanceCount  = pStatus->usLoad;
        }
        break;
    default:
        {
            assert(nServerIndex == PAKT_CENTER);
            m_uCenterTotal  = pStatus->uTotal;
            m_uCenterOnline = pStatus->uOnline;
            m_uCenterLoad   = pStatus->usLoad / DATAD_TOPERCENT;
        }
    }
}

void CCenterHandler::OnSync(Int, CStream&)
{
}

void CCenterHandler::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    RemoveServerData(nServerIndex, utData);
}

void CCenterHandler::UpdateServerInfo(void)
{
    CStringFix strTemp = GetName();
    strTemp.AppendFormat(TF("[Servers=%u(Linked)/%u(Connected), Load=%d%%][Balance:Count=%u, %u(AuthorAddr)/%u(WLanAddr)]")
                         TF("[Author:Count=%u, %u(Linked)/%u(Connected)][Master:Count=%u, %u(Gates)/%u(Services)]")
                         TF("[AuthorDB:Count=%u, %u(Authors)/%u(DBQuery)][MasterDB:Count=%u, %u(Servers)/%u(DBQuery)]"),
                         m_uCenterOnline, m_uCenterTotal,     m_uCenterLoad,
                         m_uBalanceCount,  m_uBalanceAddr,    m_uBalanceNotice,
                         m_uAuthorCount,   m_uAuthorOnline,   m_uAuthorTotal,
                         m_uMasterCount,   m_uMasterGate,     m_uMasterService,
                         m_uAuthorDBCount, m_uAuthorDBServer, m_uAuthorDBQuery,
                         m_uMasterDBCount, m_uMasterDBServer, m_uMasterDBQuery);
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

PCXStr CCenterHandler::GetName(void)
{
    return TF("Center:");
}




